<template>
  <div>
    <el-tabs :tab-position="'right'" style="height: 200px">
      <el-tab-pane label="饼图">
        <div
          id="charts-pie3"
          style="width: 400px; height: 300px; margin-right: 40px"
        ></div>
      </el-tab-pane>
      <el-tab-pane label="折线图">
        <div
          id="charts-line3"
          style="width: 400px; height: 300px; margin-right: 40px"
        ></div>
      </el-tab-pane>
      <el-tab-pane label="柱状图">
        <div
          id="charts-bar3"
          style="width: 400px; height: 300px; margin-right: 40px"
        ></div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getQuestionCount, getQuestionList } from '@/api/form'

export default {
  name: 'switchWrapper',
  props: {
    res: {
      required: true
    }
  },
  data() {
    return {
      close: 0,
      open: 0,
      pinlv: [],
      xData: [],
      yData: []
    }
  },
  async created() {
    const { data } = await getQuestionList(this.res.field)
    console.log(data)
    for (var index in data.data) {
      if (data.data[index]) {
        this.open++
      } else {
        this.close--
      }
      this.pinlv.push({ 'value': index, 'name': data.data[index] })
      this.xData.push(index)
      this.yData.push(data.data[index])
    }
    // 基于准备好的dom，初始化echarts实例
    var myChart_pie3 = this.$echarts.init(document.getElementById('charts-pie3'));

    // 指定图表的配置项和数据
    var option_pie3 = {
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'verticatl',
        left: 'right',
      },
      grid: {
        left: '200px'
      },
      series: [
        {
          name: '访问来源',
          type: 'pie',
          radius: '80%',
          data: [{
            name: '开启',
            value: this.open
          },
          {
            name: '关闭',
            value: this.close
          }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart_pie3.setOption(option_pie3);

    var myChart_line3 = this.$echarts.init(document.getElementById('charts-line3'));

    // 指定图表的配置项和数据
    var option_line3 = {
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'verticatl',
        left: 'right',
      },
      grid: {
        left: '200px'
      },
      xAxis: {
        type: 'category',
        data: ['开启', '关闭']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: '访问来源',
          type: 'line',
          radius: '80%',
          data: [{
            name: '开启',
            value: this.open
          },
          {
            name: '关闭',
            value: this.close
          }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart_line3.setOption(option_line3);

    var myChart_bar3 = this.$echarts.init(document.getElementById('charts-bar3'));

    // 指定图表的配置项和数据
    var option_bar3 = {
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'verticatl',
        left: 'right',
      },
      grid: {
        left: '200px'
      },
      xAxis: {
        type: 'category',
        data: ['开启', '关闭']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: '访问来源',
          type: 'bar',
          radius: '80%',
          data: [{
            name: '开启',
            value: this.open
          },
          {
            name: '关闭',
            value: this.close
          }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart_bar3.setOption(option_bar3);
  },
  methods: {
    setOptions() {
      return {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'verticatl',
          left: 'right',
        },
        grid: {
          right: '60%'
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '80%',
            data: [{
              name: '开启',
              value: this.open
            },
            {
              name: '关闭',
              value: this.close
            }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }
    },
  }
}
</script>

<style>
</style>